<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" @row-click="handleRowClick" border :height="autoHeight" highlight-current-row>
      <el-table-column prop="confName" label="名称" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="confLabel" label="标签" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="keyWord" label="关键字" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="xAxis" label="横向偏移" align="center"></el-table-column>
      <el-table-column prop="yAxis" label="纵向偏移" align="center"></el-table-column>
      <el-table-column prop="confPage" label="固定页码" align="center"></el-table-column>
      <el-table-column prop="confType" label="签章类型" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.confType === 1" type="primary">关键字</el-tag>
          <el-tag v-else-if="scope.row.confType === 2" type="warning">骑缝章</el-tag>
          <el-tag v-else-if="scope.row.confType === 3" type="success">固定位置</el-tag>
          <el-tag v-else type="danger">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="130px">
        <template v-if="picture.pictureId" slot="header">
          <el-button v-if="!select" type="primary" @click="visible = true" icon="el-icon-circle-plus-outline">新增</el-button>
        </template>
        <span v-else>操作</span>
        <div slot-scope="scope">
          <template v-if="select">
            <el-button type="success" icon="el-icon-thumb" circle @click="$emit('rowSelect',scope.row)" title="选择"></el-button>
          </template>
          <template v-else>
            <el-button type="success" icon="el-icon-edit" circle @click="() => { entityInfo=$Util.copy(scope.row);visible=true}" title="编辑"></el-button>
            <el-button type="warning" icon="el-icon-paperclip" circle @click="testSign(scope.row, scope.$index)" title="测试"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
          </template>
        </div>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>

    <el-dialog :visible.sync="visible" title="签章配置">
      <el-form label-position="right" label-width="80px">
        <el-row :gutter="8">
          <el-col :span="8">
            <el-form-item label="配置名称">
              <el-input v-model="entityInfo.confName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="配置标签">
              <el-input v-model="entityInfo.confLabel"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="配置类型">
              <el-radio-group v-model="entityInfo.confType">
                <el-radio :label="1">关键字</el-radio>
                <el-radio :label="2">骑缝章</el-radio>
                <el-radio :label="3">固定位置</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="横向偏移">
              <el-input-number v-model="entityInfo.xAxis" style="width: 100%" :precision="2" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="纵向偏移">
              <el-input-number v-model="entityInfo.yAxis" style="width: 100%" :precision="2" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <template v-if="entityInfo.confType === 1">
            <el-col :span="8">
              <el-form-item label="关键字">
                <el-input v-model="entityInfo.keyWord"></el-input>
              </el-form-item>
            </el-col>
          </template>
          <template v-else-if="entityInfo.confType === 3">
            <el-col :span="8">
              <el-form-item label="固定页码">
                <el-input-number v-model="entityInfo.confPage" :min="1" style="width: 100%" controls-position="right"></el-input-number>
              </el-form-item>
            </el-col>
          </template>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="8">
            <el-form-item label="固定高度">
              <el-input-number v-model="entityInfo.confHeight" style="width: 100%" :precision="2" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="固定宽度">
              <el-input-number v-model="entityInfo.confWidth" style="width: 100%" :precision="2" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大签章">
              <el-input-number v-model="entityInfo.signMax" style="width: 100%" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="签名范围">
              <el-input v-model="entityInfo.confRange"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="追加内容">
          <el-input type="textarea" :rows="4"  v-model="entityInfo.appendStr" placeholder="以;分割，分割顺序为上右下左，只配一个时，为下部"/>
        </el-form-item>
        <el-form-item label="签章样式">
          <el-input type="textarea" :rows="4"  v-model="entityInfo.confStyle" placeholder="签章样式"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-circle-check" type="success" @click="saveData(entityInfo)">{{ entityInfo.roleId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="testVisible" title="签名配置测试" width="400px">
      <el-upload
          :action="uploadUrl"
          :headers="headers"
          :on-success="uploadSuccess"
          :show-file-list="false"
          drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "SignConf",
  mixins: [IdeaTable],
  props: {
    select: {
      type: Boolean,
      default: false
    },
    picture: {
      type: Object,
      default: function () {
        return { pictureId: '0' }
      }
    }
  },
  data() {
    return {
      testConf: {},
      testVisible: false
    }
  },
  computed:{
    autoHeight() {
      return `calc(100vh - 185px)`
    },
    uploadUrl () {
      return this.$Util.mergeUrl(process.env.VUE_APP_BASE_API,  '/signConf/test/' + this.testConf.confId)
    },
    headers () {
      return { Authorization: 'Bearer ' + this.$AuthStorage.getAccessToken() }
    }
  },
  created() {
    this.queryUrl = '/signConf/queryPage'
    this.saveUrl = '/signConf/save'
    this.removeUrl = '/signConf/delete/'
    this.rowKey = 'confId'
    this.addFilter({ field: 'pictureId', op: '=', value: this.picture.pictureId || '0' })
    this.loadSource()
  },
  methods: {
    initEntity(){
      this.$Util.cleanObj(this, this.entityInfo)
      this.$Util.copyAttr({ confType: 1, pictureId: this.picture.pictureId, signMax: 20, xAxis: 0, yAxis: 0 }, this.entityInfo, null, this)
    },
    testSign(row) {
      this.$Util.cleanObj(this, this.testConf)
      this.$Util.copyAttr(row, this.testConf,null,this)
      this.testVisible = true
    },
    uploadSuccess (response) {
      this.$resultMsg(this, response)
      if (response.code === 200) {
        const url = this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/signRecord/preview/' + response.data.recordId + '?access_token=' + this.$AuthStorage.getAccessToken())
        window.open(url)
        this.testVisible = false
      }
    }
  }
}
</script>

<style scoped>

</style>
